Skip to content

Conversation

oroztocil
Copy link
Member

@oroztocil oroztocil commented Apr 28, 2025

This is a work-in-progress implementation of AsFunction extension methods for IJSObjectReference. These methods create C# delegates that can be used as typed wrappers for invoking referenced JS functions via interop. Currently, it adds overloads for void/value-returning functions with up to 3 arguments.

This is a reflection-free alternative to #61673.
The downside for this simpler implementation is the need to introduce multiple overloads into the public API.

@ghost ghost added the area-blazor Includes: Blazor, Razor Components label Apr 28, 2025
/// <param name="jsObjectReference">The JavaScript object reference that represents the function to be invoked.</param>
/// <returns>A delegate that can be used to invoke the JavaScript function.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="jsObjectReference"/> is null.</exception>
public static Func<ValueTask> AsVoidFunction(this IJSObjectReference jsObjectReference)
Copy link
Member

@pavelsavara pavelsavara Apr 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is guess ValueTask (vs Task) is here for consistency ?

Copy link
Member Author

@oroztocil oroztocil May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because the other Invoke* methods return ValueTask. The JSRuntime method which implements the actual call also returns ValueTask (although it creates it from a regular Task, for some reason).

@pavelsavara
Copy link
Member

pavelsavara commented Apr 28, 2025

How do I Dispose() the underlying function proxy if I only have the delegate now ?

Are we going to take the DX convenience to next level ? What about passing JS and C# functions as callbacks to other functions ?

@oroztocil
Copy link
Member Author

oroztocil commented May 5, 2025

How do I Dispose() the underlying function proxy if I only have the delegate now ?

To create the delegate, you must have had the IJSObjectReference returned from and interop call somewhere and you are responsible for disposing that just like you always were.

Are we going to take the DX convenience to next level ? What about passing JS and C# functions as callbacks to other functions ?

You can now pass JS functions (as IJSObjectReference) back to JS to use as callbacks.

There is no special support for passing C# functions. You have to create a JS wrapper and pass it the string identifiers of the assembly/method to call a static method, or passing a DotNetObjectReference + string identifier to call an instance method. I don't know if there is any demand for making this more streamlined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants